from template import *
from collect import collect


import sys

itime = 0
if len(sys.argv) == 2:
	t = int(sys.argv[1])
elif len(sys.argv) > 2:
	print("error: should have one argument, as time step")
else:
	t = itime



amplification_factor = 100.0

nx = 200

dx = 0.5e-5  # unit (m)
x = np.linspace(0, nx * dx, nx)
x = x * amplification_factor

xmin = x.min()
xmax = x.max()

list_fe_sheath = []
list_fe_mid = []
list_fe_source = []
list_fD1_sheath = []
list_fD1_mid = []
list_fD1_source = []

list_label = ["Rc = 0", "Rc = 20", "Rc = 40", "Rc = 60", "Rc = 80", "Rc = 100"]

list_ylabel = ["f_e(source)", "f_D1(source)", "f_e(mid)", "f_D1(mid)", "f_e(sheath)", "f_D1(sheath)"]

list_path = ["sheath_limit_cc0/data", "sheath_limit_cc20/data", "sheath_limit_cc40/data", "sheath_limit_cc60/data", "sheath_limit_cc80/data", "sheath_limit/data"]
for path_item in list_path:
    val = collect("/Diagnostic/", "vdf_parallel_to_B", path = path_item)[t, :, :, :]

    list_fe_sheath.append(val[0, 0, :])
    list_fe_mid.append(val[3, 0, :])
    list_fe_source.append(val[5, 0, :])

    list_fD1_sheath.append(val[0, 1, :])
    list_fD1_mid.append(val[3, 1, :])
    list_fD1_source.append(val[5, 1, :])



##inite the fig of matplotlib
fig=plt.figure(figsize=(15,10))
fig.subplots_adjust(top=0.9,bottom=0.1,wspace=0.5,hspace=0.35)

##============potential======================================================
ax=fig.add_subplot(3,2,1)

for i in range(len(list_fe_source)):
    line0=ax.plot(x, list_fe_source[i], label = list_label[i])

ax.set_ylim(bottom = 0.0)
ax.set_ylabel(list_ylabel[0], fontsize = label_fontsize)
#ax.grid(True)

num1 = 0
num2 = 1.4
num3 = 2
num4 = 0
handles, labels = ax.get_legend_handles_labels()
ax.legend(flip(handles, 3), flip(labels, 3), framealpha = 0.2, bbox_to_anchor=(num1, num2), loc=num3, borderaxespad=num4, ncol = 3)


ax.annotate("(a)", xy=get_axis_limits(ax, x_scale = -0.1, y_scale = -0.15), annotation_clip=False)

##============density======================================================
ax=fig.add_subplot(3,2,2)

for i in range(len(list_fD1_source)):
    line0=ax.plot(x, list_fD1_source[i], label = list_label[i])

ax.set_ylim(bottom = 0.0)
ax.set_ylabel(list_ylabel[1], fontsize = label_fontsize)
#ax.grid(True)
#ax.legend(framealpha = 0.2)
ax.annotate("(b)", xy=get_axis_limits(ax, x_scale = -0.1, y_scale = -0.15), annotation_clip=False)

##============ Te ======================================================
ax=fig.add_subplot(3,2,3)

for i in range(len(list_fe_mid)):
    line0=ax.plot(x, list_fe_mid[i], label = list_label[i])

ax.set_ylim(bottom = 0.0)
ax.set_ylabel(list_ylabel[2], fontsize = label_fontsize)
#ax.grid(True)
#ax.legend(framealpha = 0.2)
ax.annotate("(c)", xy=get_axis_limits(ax, x_scale = -0.1, y_scale = -0.15), annotation_clip=False)



##============TD1======================================================
ax=fig.add_subplot(3,2,4)

for i in range(len(list_fD1_mid)):
    line0=ax.plot(x, list_fD1_mid[i], label = list_label[i])

ax.set_ylim(bottom = 0.0)
ax.set_ylabel(list_ylabel[3], fontsize = label_fontsize)
#ax.grid(True)
#ax.legend(framealpha = 0.2)
ax.annotate("(d)", xy=get_axis_limits(ax, x_scale = -0.1, y_scale = -0.15), annotation_clip=False)


##============qe======================================================
ax=fig.add_subplot(3,2,5)

for i in range(len(list_fe_sheath)):
    line0=ax.plot(x, list_fe_sheath[i], label = list_label[i])

#ax.set_ylim(bottom = 0.0)
ax.set_ylabel(list_ylabel[4], fontsize = label_fontsize)
#ax.grid(True)
#ax.legend(framealpha = 0.2)
ax.annotate("(e)", xy=get_axis_limits(ax, x_scale = -0.1, y_scale = -0.15), annotation_clip=False)


##============qD1======================================================
ax=fig.add_subplot(3,2,6)

for i in range(len(list_fD1_sheath)):
    line0=ax.plot(x, list_fD1_sheath[i], label = list_label[i])

#ax.set_ylim(bottom = 0.0)
ax.set_ylabel(list_ylabel[5], fontsize = label_fontsize)
#ax.grid(True)
#ax.legend(framealpha = 0.2)
ax.annotate("(f)", xy=get_axis_limits(ax, x_scale = -0.1, y_scale = -0.15), annotation_clip=False)


fig_file_name = "vdf_all_" + str(t) + ".svg"
fig.savefig(fig_file_name, dpi = 300)
##fig.show()       #when the program finishes,the figure disappears
#plt.axis('equal')
#plt.show()         #The command is OK
